jQuery(function() {
    jQuery("#superior").select2().trigger("change");
	//查看机会信息
	jQuery(".seechance").on("click",function(){
    	jQuery("#chanceInfoModal").modal('show');
    });
	//查看跟踪信息
	jQuery(".followbtn").on("click",function(){
    	jQuery("#followInfoModal").modal('show');
    });
	//查看合同
	jQuery(".seecontract").on("click",function(){
		window.location = window.rootPath+"/ui/contract/newContract";
    });
	jQuery('#customerName').change(function(){
		var customerName = jQuery("#customerName").val();
		jQuery("#postUnit").val(customerName);
		jQuery("#cUnitName").val(customerName);
	});
	jQuery('#address').change(function(){
		var address = jQuery("#address").val();
		jQuery("#invoiceAddress").val(address);
		jQuery("#postAddress").val(address);
		jQuery("#cPhoneNumber").val(address);
	});
	
	jQuery('#cDHPhone').change(function(){
		var invoicePhone = jQuery("#cDHPhone").val();
		jQuery("#postPhone").val(invoicePhone);
		jQuery("#invoicePhone").val(invoicePhone);
		jQuery("#cSHPhoneName").val(invoicePhone);
	});
	
	jQuery('#zipCode').change(function(){
		var postZip = jQuery("#zipCode").val();
		jQuery("#cSHYB").val(postZip);
		jQuery("#postZip").val(postZip);
	});
	
	///////计算客户信用得分计算
	
	jQuery('#asset').change(function(){
		getScore();
	});
	jQuery('#continuelife').change(function(){
		getScore();
	});
	jQuery('#buyer').change(function(){
		getScore();
	});
	jQuery('#credit').change(function(){
		getScore();
	});
	jQuery('#period').change(function(){
		getScore();
	});
	jQuery('#business').change(function(){
		getScore();
	});
	jQuery('#score').blur(function(){
		getScore();
	})
	getScore();
	////计算客户信用得分
	function getScore(){
		var asset = parseInt(jQuery("#asset").val());
		var continuelife = parseInt(jQuery("#continuelife").val());
		var buyer = parseInt(jQuery("#buyer").val());
		var credit = parseInt(jQuery("#credit").val());
		var period=parseInt(jQuery("#period").val());
		var score=jQuery("#score").val();
		if(score=='') score='0'; 
		score = parseInt(score); 
		var business = parseInt(jQuery("#business").val());
		var creditLevel=asset+continuelife+buyer+credit+period+score+business;
		if(creditLevel == null || creditLevel == undefined || isNaN(creditLevel)){
			jQuery('#creditLevel').val("");
		}else{
			jQuery('#creditLevel').val(creditLevel);
		}
	}
	/////////////////////////////////计算
	
	
//	/////客户编号验证
//	jQuery('#customerCode').blur(function(){
//		var customerId
//		var customerCode=jQuery("#customerCode").val();
//		var url=window.rootPath+"/ui/customer/codevalidate"+"?customerCode="+customerCode;
//		
//		jQuery.ajax({
//			url:  encodeURI(url),
//			cache:false,
//            type: 'get',
//            dataType: 'json',
//            success: function (data) {
//            	if(data.code==0){
//            		$.springLemonDialog({
//   	 		         content: '编号重复',// 确认文本
//   	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
//   	 		             $(this).closeDialog(modal); 
//   	 		         }
//   	 		        });
//            	}	
//            }
//        });		
//	})
	
	/*$.springLemonDialog({
        content: '即将保存该界面信息，是否继续？',// 确认文本
        clickButton: function (sender, modal, index) {// 按钮事件处理
            $(this).closeDialog(modal);
  		if(index == 0){
			if (jQuery("#son_Form") && !jQuery("#son_Form").Validform({
				tiptype : 3,
				showAllError : true
			}).check(false)) {return;}
			jQuery("#son_save").attr("disabled","disabled");
			var api = window.rootPath + "/ui/beforecontract/saveExdeBeforeContractDetailParts";
			var d={
					"id" : $("#idson").val(),
					"bContractDetailId" : $("#id").val(),
					"nOrderNumber" : $("#nOrderNumber").val(),
					"cName" : $("#cName").val(),
					"nCount" : $("#nCount").val(),
					"nContractPrice" : $("#nContractPrice").val(),
					"nOutPrice" : $("#nOutPrice").val(),
					"nAddOrReducePrice" : $("#nAddOrReducePrice").val()
			};
			jQuery.ajax({
				type : 'post',
				url : api,
				data : JSON.stringify(d),
				dataType : 'json',
				contentType : 'application/json;charset=utf-8',
				success : function(data) {
					serverData2.beforeDetailId = $('#id').val();
					serverTable2.draw();
					serverData1.beforeId = $('#beforeId').val();
					serverTable1.draw();
					jQuery("#son_save").removeAttr("disabled");
					jQuery('#payInfo').modal('hide');
					jQuery('#nTotalContractPrice').val(data.list[0]);
					jQuery('#nDisCountPercent').val(data.list[1]);
				}
			});
		}
    }
});*/
	//////////////////////////客户信息///////////////////////////////////////////////
	//保存客户信息
	function saveCustomer(isreturn,type){//isreturn==1,直接返回customerindex；isreturn==2在保存客户信息后留着当前页
	        		var id = jQuery("#customerId").val();
					var customercreateTime=jQuery("#customercreateTime").val();
					var createTime=new Date(customercreateTime).getTime() ;
					var customerCode = jQuery('#customerCode').val();
					var customerName = jQuery('#customerName').val();
					var anotherName=jQuery('#anotherName').val();
					var areaId = jQuery('#saleArea').val();
					var business = jQuery("#business").find("option:selected").text();
					var cDHPhone=jQuery("#cDHPhone").val();
					var cDHDB=jQuery("#cDHDB").val();
					var address = jQuery("#address").val();
			        var superior = jQuery("#superior").val();
					var baseMemo = jQuery("#baseMemo").val();
					
					var asset = jQuery("#asset").find("option:selected").text(); 
					var continuelife = jQuery("#continuelife").find("option:selected").text();
					var buyer = jQuery("#buyer").find("option:selected").text();
					var credit = jQuery("#credit").find("option:selected").text();
					var period=jQuery("#period").find("option:selected").text();
					var score = jQuery("#score").val();
					var creditLevel = jQuery("#creditLevel").val();
	        		
					var url = window.rootPath + "/ui/customer/saveCustomer?customerId="+id;
					jQuery(".btn").attr("disabled", true);
					// 判断是编辑还是新增 
					if (id == "") {
						var d = {
							"customerCode" : customerCode,
							"customerName" : customerName,
							"anotherName" : anotherName,
			  	          /**
			  	            "postUnit" : postUnit,
			  	            "postDelegate" : postDelegate,
			  	            "postPhone" : postPhone,
			  	            "postAddress" : postAddress,
			  	            "postZip" : postZip,
			  	            
			  	            **/
			  	            
							"business" : business,
							"saleArea": {
			  	                "id": areaId
			  	            },
							"superior" : superior,
							"cDHPhone" : cDHPhone,
							"cDHDB" : cDHDB,
							"address" : address,
							"baseMemo" : baseMemo,
							
							"asset" : asset,
							"continuelife":continuelife,
							"buyer" : buyer,
							"credit" : credit,
							"period":period,
							"score" : score,
							"creditLevel" : creditLevel,
						
							/**
							"ownedSum" : ownedSum,	
							"identification" : identification,
							"invoicePhone" : invoicePhone,
							"zipCode" : zipCode,
							"invoiceAddress" : invoiceAddress,
							"invoiceBank" : invoiceBank,
							"bankAccount" : bankAccount,
							"taxNumber" : taxNumber,
							"invoiceMemo" : invoiceMemo,
							"cUnitName" : cUnitName,
							"cPhoneNumber" : cPhoneNumber,
							"cSHName" : cSHName,
							"cSHPhoneName" : cSHPhoneName,
							"cSHYB" : cSHYB,
							**/
						};
						
					} else {
						var d = {
							"id" : id,
							"createTime":createTime,
							"customerCode" : customerCode,
							"customerName" : customerName,
							"anotherName" : anotherName,

							/**
			  	            "postUnit" : postUnit,
			  	            "postDelegate" : postDelegate,
			  	            "postPhone" : postPhone,
			  	            "postAddress" : postAddress,
			  	            "postZip" : postZip,
			  	            
							**/
			  	            
							"business" : business,
							"saleArea": {
			  	                "id": areaId
			  	            },
							"superior" : superior,
							"cDHPhone" : cDHPhone,
							"cDHDB" : cDHDB,
							"address" : address,
							"baseMemo" : baseMemo,
							"asset" : asset,
							"continuelife":continuelife,
							"buyer" : buyer,
							"credit" : credit,
							"period":period,
							"score" : score,
							"creditLevel" : creditLevel,
							
							/**
							"ownedSum" : ownedSum,	
							"identification" : identification,
							"invoicePhone" : invoicePhone,
							"zipCode" : zipCode,
							"invoiceAddress" : invoiceAddress,
							"invoiceBank" : invoiceBank,
							"bankAccount" : bankAccount,
							"taxNumber" : taxNumber,
							"invoiceMemo" : invoiceMemo,
							"cUnitName" : cUnitName,
							"cPhoneNumber" : cPhoneNumber,
							"cSHName" : cSHName,
							"cSHPhoneName" : cSHPhoneName,
							"cSHYB" : cSHYB,
							
							**/
						};
					}
					
					jQuery.ajax({
			            type: 'post',
			            url:  encodeURI(url),
			            data: JSON.stringify(d),
			            dataType: 'json',
			            contentType: 'application/json;charset=utf-8',
			            success: function (data) {
			            	jQuery("#customerId").val(data.customerId);
			            	jQuery(".btn").attr("disabled", false);
			            	if(isreturn==1){
			            		window.location = window.rootPath+"/ui/customer";
//			            		mailingInfoData.CustomerId=data.customerId;
//			            		payInfoTable.draw();
//			            		if(type != 1){
//			            			jQuery("#"+type).modal('show');
//			            		}
			            		
			            	}
			            	else if(isreturn==2){
			            		var customerId=data.customerId;
			            		var time=moment(data.createTime).format("YYYY-MM-DD HH:mm:ss")
			            		jQuery("#customercreateTime").val(time);
			            		savekeycontact(customerId);
			            		payInfoData.CustomerId=data.customerId;
			            		payInfoTable.draw();
			            		if(type != 1){
			            			jQuery("#"+type).modal('show');
			            		}
			            	}else if(isreturn==3){
			            		var time=moment(data.createTime).format("YYYY-MM-DD HH:mm:ss")
			            		jQuery("#customercreateTime").val(time);
			            		payInfoData.CustomerId=jQuery("#customerId").val();
			            		mailingInfoData.CustomerId=jQuery("#customerId").val();
			            		sendInfoData.CustomerId=jQuery("#customerId").val();
			            		payInfoTable.draw();
			            		mailingInfoTable.draw();
			            		sendInfoTable.draw();
			            		jQuery("#"+type).modal('show');
			            	}
			            	
			            }
					
			        });
	        		
	        	
				
				/**
				var identification = jQuery("#identification").val();
				var invoicePhone = jQuery("#invoicePhone").val();
				var zipCode = jQuery("#zipCode").val();
				var invoiceBank = jQuery("#invoiceBank").val();
				var invoiceAddress = jQuery("#invoiceAddress").val();
				var bankAccount = jQuery("#bankAccount").val();
				var taxNumber = jQuery("#taxNumber").val();
				var invoiceMemo=jQuery("#invoiceMemo").val();
				
				
				
				
				
				
				
				var postUnit=jQuery("#postUnit").val();
				var postDelegate=jQuery("#postDelegate").val();
				var postPhone=jQuery("#postPhone").val();
				var postAddress=jQuery("#postAddress").val();
				var postZip=jQuery("#postZip").val();
				
				
				var cUnitName=jQuery("#cUnitName").val();
				var cPhoneNumber=jQuery("#cPhoneNumber").val();
				var cSHName=jQuery("#cSHName").val();
				var cSHPhoneName=jQuery("#cSHPhoneName").val();
				var cSHYB=jQuery("#cSHYB").val();
				**/
	   
	}
	// 保存客户信息
	jQuery('.saveBtn').on('click', function() {
		
		if (jQuery("#customerEdit") && !jQuery("#customerEdit").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {
			return;
		};	
		
		
		//客户编号验证
		var d={
				"id":jQuery("#customerId").val(),
				"customerCode":jQuery("#customerCode").val()
		}
		var url=window.rootPath+'/ui/customer/codeValidate';
		jQuery.ajax({
			url:encodeURI(url),
			type:'get',
			data:d,
			dataType:'json',
			success:function(data){
				if(data.code>0){
					message("客户编号重复");
				}else{
					saveCustomer(1,1);
				}
			}
		})
			
	});
	
	
	//////////////////////////////客户关键人物/////////////////////////////////
	///保存关键人物//////////
	function savekeycontact(customerId){
		var id=jQuery("#keyId").val();
		var keycreateTime=jQuery("#keycreateTime").val();
		var keyname=jQuery("#keyname").val();
		var calledName=jQuery("#calledName").val();
		var contactType=jQuery('#contactType').val();
		var postion=jQuery('#postion').val();
		var major=jQuery('#major').val();
		var domain=jQuery('#domain').val();
		var memo=jQuery('#memo').val();
		var url1=window.rootPath+"/ui/customer/saveKeyContact";
		var d;
		if(id==''){
	      	  d = {
	      			"name": keyname,
	   	            "calledName": calledName,
	   	            "contractType": contactType,
	   	            "position": postion,
	   	            "major": major,
	   	            "domain": domain,
	   	            "memo": memo,
	   	            "customer": {
	   	                "id": customerId
	   	            } 
	   	        };
	      	  	jQuery.ajax({
		          type: 'post',
		          url: encodeURI(url1),
		          data: JSON.stringify(d),
		          dataType: 'json',
		          contentType: 'application/json;charset=utf-8',
		          success: function (data) {
		        		jQuery('#customerkeyModal').modal('hide');
		        		keyContactData.customerId=customerId;
		        		jQuery('#customerId').val(customerId);
		        		keyContactTable.draw();
		          }
	      	  	});
	      }else{
	      	d = {
	   	            "id":id,
	   	            "createTime":keycreateTime,
	   	            "name": keyname,
	   	            "calledName": calledName,
	   	            "contractType": contactType,
	   	            "position": postion,
	   	            "major": major,
	   	            "domain": domain,
	   	            "memo": memo,
		            "customer": {
		                "id": customerId
		            } 
	   	        };
	      	jQuery(".btn").attr("disabled", true);
	      	jQuery.ajax({
		          type: 'post',
		          url:  encodeURI(url1),
		          data: JSON.stringify(d),
		          dataType: 'json',
		          contentType: 'application/json;charset=utf-8',
		          success: function (data) {
		        	  jQuery(".btn").attr("disabled", false);
		          	  jQuery('#customerkeyModal').modal('hide');
		          	  jQuery('#customerId').val(customerId);
		          	  keyContactTable.draw(false);
		          }
		      });
	      }
		
		
	};
	
	////获得关键联系人信息 服务器分页
	 var keyContactData = {};
	 var keyid=jQuery('#customerId').val();
	 
	 keyContactData.customerId=keyid;
	 var keyContactTable;
	 
	 var payInfoData = {};
     var payInfoTable;
     
     var mailingInfoData = {};
     var mailingInfoTable;
     
     var sendInfoData = {};
     var sendInfoTable;
	    jQuery(document).ready(function () {
	    	/**
	    	 * 获取开票信息列表中的数据
	    	 */
	    	var CustomerId = jQuery('#customerId').val();
    		payInfoData.CustomerId=jQuery("#customerId").val();
    		payInfoData.key="";
    		payInfoTable = jQuery('#payInfoTable').DataTable({
            	"serverSide": true,
            	"ordering": false,
            	//"pageLength" : -1,
            	"ajax": {
            		"dataType": "json",
            		"type": "post",
            		"cache": false,
            		"url":window.rootPath+ '/ui/customer/getPayInfoList',
            		"data": function (d) {
            			return $.extend({}, d, payInfoData);
            		}
            	},
                "columns" : [{
    			                data: null,
    			                defaultContent : ""// 排序   1
    			            },
    			            {
                	            data: null,
                	            defaultContent: "",
                	            render: function (data, type, full, meta) {
                	            	var id=data.id
                	            	var operate ='<input type="checkbox" class="checkChildPay">';
                	            	return operate;
                	            }   
                            },
                            {
            	                data: 'identification'
            	            },
            	            {
            	                data: 'invoicePhone'
            	            },
            	            {
            	            	data: 'invoiceFax'
            	            },
            	            {
            	                data: 'zipCode'
            	            },
            	            {
            	                data: 'invoiceAddress'
            	            },
            	            {
            	                data: 'invoiceBank'
            	            },
            	            {
            	                data: 'bankAccount'
            	            },
            	            /*{
            	                data: 'taxNumber'
            	            },*/
            	            {
            	                data: 'invoiceMemo'
            	            },
            	            {
            	            	data: 'isDefaultHost',
            	            	 render: function (data, type, full, meta) {
            	            		 var operate="";
            	            		 if(data==1) operate='<span class="badge label-pill label-primary">默认</span>';
                 	            	return operate;
                 	            } 
            	            },
            	            {
            	            	data: 'isDefaultPart',
            	            	 render: function (data, type, full, meta) {
            	            		 var operate="";
            	            		 if(data==1) operate='<span class="badge label-pill label-primary">默认</span>';
                 	            	return operate;
                 	            } 
            	            }
            	            
            	            ],
                "drawCallback": function (oSettings) {
                    var k = 0;
                    var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                    for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                        $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                        k++;
                    }
                }
            	
            });
    		
    		
    		
    		jQuery('#pay-search-btn').on('click',function(){
    	    	var key=jQuery('#pay-key-word').val();
    	    	payInfoData.key=key;
    	    	payInfoTable.draw();
            })
            //撤销查询
            jQuery('#pay-reset-btn').on("click",function()
            {
            	jQuery('#pay-key-word').val('');
            	payInfoData.key =jQuery('#pay-key-word').val();
                payInfoTable.draw();
            });
    		
    		
    		
    		
    		
    		jQuery('#mailing-search-btn').on('click',function(){
    	    	var key=jQuery('#mailing-key-word').val();
    	    	mailingInfoData.key=key;
    	    	mailingInfoTable.draw();
            })
            //撤销查询
            jQuery('#mailing-reset-btn').on("click",function()
            {
            	jQuery('#mailing-key-word').val('');
            	mailingInfoData.key =jQuery('#mailing-key-word').val();
            	mailingInfoTable.draw();
            });
    		//邮寄信息列表
    		mailingInfoData.CustomerId=jQuery("#customerId").val();
    		mailingInfoData.key="";
    		mailingInfoTable = jQuery('#mailingInfoTable').DataTable({
    			"serverSide": true,
    			"ordering": false,
    			//"pageLength" : -1,
    			"ajax": {
    				"dataType": "json",
    				"type": "post",
    				"cache": false,
    				"url":window.rootPath+ '/ui/customer/getMailingInfoList',
    				"data": function (d) {
    					return $.extend({}, d, mailingInfoData);
    				}
    			},
    			"columns" : [{
    				data: null,
    				defaultContent : ""// 排序   1
    			},
    			{
    				data: null,
    				defaultContent: "",
    				render: function (data, type, full, meta) {
    					var id=data.id
    					var operate ='<input type="checkbox" class="checkChildMailing">';
    					return operate;
    				}   
    			},
    			{
    				data: 'postUnit'
    			},
    			{
    				data: 'postDelegate'
    			},
    			{
    				data: 'postPhone'
    			},
    			{
    				data: 'postZip'
    			},
    			{
    				data: 'postAddress'
    			},
	            {
	            	data: 'isDefaultHost',
	            	 render: function (data, type, full, meta) {
	            		 var operate="";
	            		 if(data==1) operate='<span class="badge label-pill label-primary">默认</span>';
     	            	return operate;
     	            } 
	            },
	            {
	            	data: 'isDefaultPart',
	            	 render: function (data, type, full, meta) {
	            		 var operate="";
	            		 if(data==1) operate='<span class="badge label-pill label-primary">默认</span>';
     	            	return operate;
     	            } 
	            }],
    			"drawCallback": function (oSettings) {
    				var k = 0;
    				var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
    				
    				for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
    					$('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
    					k++;
    				}
    			}
    			
    		});
    		
    		
    		
    		
    		jQuery('#send-search-btn').on('click',function(){
    	    	var key=jQuery('#send-key-word').val();
    	    	sendInfoData.key=key;
    	    	sendInfoTable.draw();
            })
            //撤销查询
            jQuery('#send-reset-btn').on("click",function()
            {
            	jQuery('#send-key-word').val('');
            	sendInfoData.key =jQuery('#send-key-word').val();
            	sendInfoTable.draw();
            });
    		//发货信息列表
    		sendInfoData.CustomerId=jQuery("#customerId").val();
    		sendInfoData.key="";
    		sendInfoTable = jQuery('#sendInfoTable').DataTable({
    			"serverSide": true,
    			"ordering": false,
    			//"pageLength" : -1,
    			"ajax": {
    				"dataType": "json",
    				"type": "post",
    				"cache": false,
    				"url":window.rootPath+ '/ui/customer/getSendInfoList',
    				"data": function (d) {
    					return $.extend({}, d, sendInfoData);
    				}
    			},
    			"columns" : [{
    				data: null,
    				defaultContent : ""// 排序   1
    			},
    			{
    				data: null,
    				defaultContent: "",
    				render: function (data, type, full, meta) {
    					var id=data.id
    					var operate ='<input type="checkbox" class="checkChildSend">';
    					return operate;
    				}   
    			},
    			{
    				data: 'cUnitName'
    			},
    			{
    				data: 'cSHYB'
    			},
    			{
    				data: 'cSHName'
    			},
    			{
    				data: 'cSHPhoneName'
    			},
    			{
    				data: 'cPhoneNumber'
    			},
	            {
	            	data: 'isDefaultHost',
	            	 render: function (data, type, full, meta) {
	            		 var operate="";
	            		 if(data==1) operate='<span class="badge label-pill label-primary">默认</span>';
     	            	return operate;
     	            } 
	            },
	            {
	            	data: 'isDefaultPart',
	            	 render: function (data, type, full, meta) {
	            		 var operate="";
	            		 if(data==1) operate='<span class="badge label-pill label-primary">默认</span>';
     	            	return operate;
     	            } 
	            }],
    			"drawCallback": function (oSettings) {
    				var k = 0;
    				var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
    				
    				for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
    					$('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
    					k++;
    				}
    			}
    			
    		});
    	
	    	
	    	keyContactTable = jQuery('#keyTable').DataTable({
	            "serverSide": true,
	            "iDisplayLength":5,
	            "ordering": false,
	            "ajax": {
	                "dataType": "json",
	                "type": "get",
	                "cache": false,
	                "url":window.rootPath+ '/ui/customer/getKeyContact',
	                "data": function (d) {
	                    return $.extend({}, d, keyContactData);
	                }
	            },
	            columns: [
	                {
	                    data: null,
	                    defaultContent: ""//排序
	                },
	                {
	                    data: null,
	                    defaultContent: "",
	                    render: function (data, type, full, meta) {
	    	            	var id=data.id
	    	            	var operate ='<input type="checkbox" class="checkchild">';
	    	            	return operate;
	    	            }   
	                },
	                {
	                    data: 'name'
	                },
	                {
	                    data: 'calledName'
	                },
	                {
	                    data: 'contractType'
	                },	             
	                {
	                    data: 'position'
	                },
	                {
	                    data: 'major'
	                },
	                {
	                    data: 'domain'
	                },
	                {
	                    data: 'memo'
	                }
	                ],
	            "drawCallback": function (oSettings) {
	                var k = 0;
	                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
	                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
	                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
	                    k++;
	                }
	            }
	        });
	    });
	    
////////////编辑关键人物///////
	    jQuery('.editKeyBtn').on('click', function () {
	        var selects=getSelect("#keyTable",1);
	 	   	if(selects.length>1){
	 		   $.springLemonDialog({
	 		         content: '只能编辑一条记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	 	   	}
	 	   	else if(selects.length==0){
	 		   $.springLemonDialog({
	 		         content: '请选择要编辑的记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	 	   }else if(selects.length==1){
		 		jQuery("#keyId").val(selects[0].id);
				jQuery("#keyname").val(selects[0].name);
				jQuery("#calledName").val(selects[0].calledName);
				jQuery('#contactType').val(selects[0].contractType);
				jQuery('#postion').val(selects[0].position);
				jQuery('#major').val(selects[0].major);
				jQuery('#domain').val(selects[0].domain);
				jQuery('#keycreateTime').val(selects[0].createTime);
				jQuery('#memo').val(selects[0].memo);
	      		jQuery('#customerkeyModal').modal('show');
	 	   }  
	 	});
////////删除关键人物//////
	    jQuery('.delKeyBtn').on('click', function () {
	 	   var selects=getSelect("#keyTable",1);
	 	   var url=window.rootPath+"/ui/customer/delKey ";
	 	   delSelect(selects,url,keyContactTable) 
	 	});   
	    
	    
/////////////关键人物查询
	    jQuery('.searchKeyBtn').on('click',function(){
	    	var key=jQuery('#keyContactword').val();
	    	keyContactData.key=key;
            keyContactTable.draw();
        })
        //撤销查询
        jQuery('.resetKeyBtn').on("click",function()
        {
            jQuery('#keyContactword').val('');
            keyContactData.key ='';
            keyContactTable.draw();
        });
	    ///////关键人物列表中的全选
	    $("#checkAll").click(function(){
	    	var check = $(this).prop("checked");
	        $(".checkchild").prop("checked", check);
		});
	    
	    //保存客户关键人物
		jQuery('.saveKeyBtn').on('click',function(){
			if (jQuery("#keyContactEdit") && !jQuery("#keyContactEdit").Validform({
				tiptype : 3,
				showAllError : true
			}).check(false)) {
				return;
			};
			saveCustomer(2,1);	
		})
		
		//新增客户关键人物
		jQuery(".addKeyBtn").on("click",function(){
			//var id=jQuery('#customerId').val();
			if (jQuery("#customerEdit") && !jQuery("#customerEdit").Validform({
				tiptype : 3,
				showAllError : true
			}).check(false)) {
				return;
			};
			jQuery("#keyId").val('');
			jQuery("#keyname").val('');
			jQuery("#calledName").val('');
			jQuery('#contactType').val('');
			jQuery('#postion').val('');
			jQuery('#major').val('');
			jQuery('#domain').val('');
			jQuery('#keycreateTime').val('');
			jQuery('#memo').val('');
   		    jQuery("#customerkeyModal").modal('show');	
	    });
		
///////////查看客户相关的销售信息////////////////////////
		var chanceTable;///客户机会表格
		var chanceData = {};
		jQuery('#t2').on('click',function(){
			if(chanceTable!=null) return;//如果chanceTable存在直接返回
			var customerId=jQuery('#customerId').val();
			chanceData.customerId=customerId;
			chanceData.key='';
			chanceTable= jQuery('#chanceTable').DataTable({
		            "serverSide": true,
		            "iDisplayLength":5,
		            "ordering": false,
		            "ajax": {
		                "dataType": "json",
		                "type": "get",
		                "cache": false,
		                "url":window.rootPath+ '/ui/chance/getcustomerchance',
		                "data": function (d) {
		                    return $.extend({}, d, chanceData);
		                }
		            },
		            columns: [
		                {
		                	data: null
		                },
		                {
		                    data: null,
		                    defaultContent: "",
		                    render: function (data, type, full, meta) {
		    	            	var id=data.id
		    	            	var operate ='<input type="checkbox" class="checkchild">';
		    	            	return operate;
		    	            }   
		                },
		                {
		                    data: 'name'
		                },
		                {
		                    data: 'sourceArea'
		                },
		                {
		                    data: 'source'
		                },
		                {
		                	data: 'sourcePower'
		                },
		                {
		                	data: 'orderAmount'
		                },
		                {
		                	data: 'projectState'
		                }
		                ],
		            "drawCallback": function (oSettings) {
		                var k = 0;
		                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

		                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
		                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
		                    k++;
		                }
		            }
		        });
		})
		
		//客户销售信息查询
		jQuery('.searchChanceBtn').on('click', function() {
			chanceData.key = jQuery('#keywordchance').val();
			var customerId=jQuery('#customerId').val();
			chanceData.customerId=customerId;
			chanceTable.draw();
		})
		// 撤销客户销售信息查询
		jQuery('.resetChanceBtn').on("click", function() {
			jQuery('#keywordchance').val('');
			chanceData.key = '';
			var customerId=jQuery('#customerId').val();
			chanceData.customerId=customerId;
			chanceTable.draw();
		});
////////////查看客户相关的合同信息////////////////////////
		var contractTable;///客户合同表格
		var contractData = {};
		jQuery('#t3').on('click',function(){
			if(contractTable!=null) return;//如果contractTable存在直接返回
			var customerId=jQuery('#customerId').val();
			contractData.customerId=customerId;
			contractData.key='';
			contractTable= jQuery('#contractTable').DataTable({
	            "serverSide": true,
	            "iDisplayLength":5,
	            "ordering": false,
	            "ajax": {
	                "dataType": "json",
	                "type": "get",
	                "cache": false,
	                "url":window.rootPath+ '/ui/contract/getCustomerContract',
	                "data": function (d) {
	                    return $.extend({}, d, contractData);
	                }
	            },
	            columns: [
		                {
		                	data: null
		                },
		                {
		                    data: 'supplierCode'
		                },
		                {
		                	data: 'contractType'
		                },
		                {
		                	 data: 'auditState',
		                     render: function (data, type, full, meta) {
		                      	var dOut;
		                      	 if(data=='审批中'){
		                      		dOut = '<td class="center" ><span class="badge label-pill label-primary">审批中</span></td>';
		                      	}
		                      	else if(data=='驳回'){
		                      		dOut = '<td class="center" ><span class="badge label-pill label-danger">驳回</span></td>';
		                      	}
		                      	else if(data=='通过'){
		                      		dOut = '<td class="center" ><span class="badge label-pill label-success">通过</span></td>';
		                      	}
		                      	else  if(data=='未提交'){
		                      		dOut = '<td class="center" ><span class="badge label-pill label-default">未提交</span></td>';
		                      	}
		                     	else  if(data=='已提交'){
		                      		dOut = '<td class="center" ><span class="badge label-pill label-warning">已提交</span></td>';
		                      	}
		      	            	return dOut;
		                      }
		                },
		                {
		                    data: 'belongArea'
		                },
		                {
		                    data: 'totalSum'
		                },
		                {
		                    data: 'scheduleDate',
		                    render: function (data, type, full, meta) {
		                        if (data)
		                            return moment(data).format('YYYY/MM/DD');
		                        else
		                            return '';
		                    }
		                },
		                {
		                    data: 'deliveryDate',
		                    render: function (data, type, full, meta) {
		                        if (data)
		                            return moment(data).format('YYYY/MM/DD');
		                        else
		                            return '';
		                    }
		                },
		                {
		                    data: 'stop'
		                },
		                {
		                    data: 'freightCharge'
		                },
		                {
		                    data: 'projectName'
		                },
		                {
		                    data: 'orderUnit'
		                },
		                {
		                    data: 'orderAddress'
		                },
		                {
		                    data: 'orderDelegate'
		                },
		                {
		                    data: 'orderPhone'
		                },
		                {
		                    data: 'orderZip'
		                },
		                {
		                    data: 'orderArea'
		                },
		                {
		                    data: 'postUnit'
		                },
		                {
		                    data: 'postAddress'
		                },
		                {
		                    data: 'postDelegate'
		                },
		                {
		                    data: 'postPhone'
		                },
		                {
		                    data: 'postZip'
		                }
	                ],
	            "drawCallback": function (oSettings) {
	                var k = 0;
	                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

	                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
	                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
	                    k++;
	                }
	            }
	        });
		})
		//客户合同信息查询
		jQuery('.search-contract-btn').on('click', function() {
			contractData.key = jQuery('#keywordcontract').val();
			var customerId=jQuery('#customerId').val();
			contractData.customerId=customerId;
			contractTable.draw();
		})
		// 撤销客户合同信息查询
		jQuery('.reset-contract-Btn').on("click", function() {
			jQuery('#keywordcontract').val('');
			contractData.key = '';
			var customerId=jQuery('#customerId').val();
			contractData.customerId=customerId;
			contractTable.draw();
		});
/////////////////查看客户相关的售后信息//////////////////////////////////////////
		var afterSaleTable;///售后表格
		var afterSaleData = {};
		jQuery('#t4').on('click',function(){
			if(afterSaleTable!=null) return;//如果afterSaleTable存在直接返回
			afterSaleData.key="";
			afterSaleData.customerId=jQuery("#customerId").val();
			afterSaleTable=jQuery("#customerserviceTable").DataTable({
				"serverSide":true,
				"iDisplayLength":5,
				"ordering": false,
	            "ajax": {
	                "dataType": "json",
	                "type": "get",
	                "cache": false,
					"url":window.rootPath+"/ui/afterSaleTicket/getAfterSalepage",
					"data":function(d){
						return $.extend({},d,afterSaleData);
					}
				},
				columns:[
				    {
				    	data:null
				    },
				    {
	                	data: null,
	                	 render: function (data, type, full, meta) {
		                        var dOut;
		                        if(data.isSubmit=="0"){
		                            dOut = '<td class="center" ><span class="badge label-pill label-danger">未提交</span></td>';
		                        }     
		                        else if(data.isSubmit=="1") {
		                            dOut = '<td class="center" ><span class="badge label-pill label-danger">退回</span></td>';
		                        }   
		                        else if(data.isSubmit=="2") {
		                            dOut = '<td class="center" ><span class="badge label-pill label-primary">已提交</span></td>';
		                        } 
		                        else if(data.isSubmit=="3") {
		                            dOut = '<td class="center" ><span class="badge label-pill label-primary">已派遣</span></td>';
		                        } 
		                        else if(data.isSubmit=="4") {
		                            dOut = '<td class="center" ><span class="badge label-pill label-primary">跟踪中</span></td>';
		                        } 
		                        else if(data.isSubmit=="5") {
		                            dOut = '<td class="center" ><span class="badge label-pill label-primary">待回访</span></td>';
		                        } 
		                        else if(data.isSubmit=="6") {
		                            dOut = '<td class="center" ><span class="badge label-pill label-success">已完成</span></td>';
		                        }
		                        return dOut;
		                    }
	                },
	                {
                	    data: null,
                	    render : function(data,type, full,meta) {
							var dOut;
							if (data.ticketState == '审批中') {
								dOut = '<td class="center" ><span class="badge label-pill label-primary">审批中</span></td>';
							} else if (data.ticketState == '驳回') {
								dOut = '<td class="center" ><span class="badge label-pill label-danger">驳回</span></td>';
							} else if (data.ticketState == '通过') {
								dOut = '<td class="center" ><span class="badge label-pill label-success">通过</span></td>';
							} else if (data.ticketState == '未提交') {
								dOut = '<td class="center" ><span class="badge label-pill label-default">未提交</span></td>';
							} else if (data.ticketState == '已提交') {
								dOut = '<td class="center" ><span class="badge label-pill label-warning">已提交</span></td>';
							}else if(data.ticketState==null){
								dOut='';
							}
							return dOut;
						}
                    },
                    {
	                	data:'afterSaleType'
	                },
                    {
	                	data:'proposer'
	                },
	                {
	                	data:'superior'
	                },
	                {
	                	data:'createTime',
	                	render: function (data, type, full, meta) {
                            if (data)
                                return moment(data).format('YYYY/MM/DD');
                            else
                                return '';
                       }
	                },
	                {
	                	data:'submiter.empLogin.userName'
	                },
	                {
	                    data: 'manageCode'
	                },
	                
	                {
	                    data: 'unitName'
	                },
	                {
	                    data: 'contractCode'
	                },
	                
	                {
	                	data:'linkMan'
	                },
	                {
	                	data:'linkManPhone'
	                },
	                {
	                	data:'serviceTime',
	                	render: function (data, type, full, meta) {
	                            if (data)
	                                return moment(data).format('YYYY/MM/DD');
	                            else
	                                return '';
	                     }
	                },
	                {
	                	data:'deviceCode'
	                },
	                {
	                	data:'deviceName'
	                },
	                {
	                	data:'deviceNumber'
	                },
	                {
	                	data: 'questionDetail'
	                }
				 ],
				 "drawCallback": function (oSettings) {
		                var k = 0;
		                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
		                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
		                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
		                    k++;
		                }
		            }
			});
			
		})
			//客户售后信息查询
		jQuery("#searchAfterSaleBtn").click(function(){
			afterSaleData.key=	jQuery("#keywordAfterSale").val();
			afterSaleData.customerId=jQuery("#customerId").val();
			afterSaleTable.draw();
		});
		//撤销客户售后信息查询
		jQuery("#resetAfterSaleBtn").click(function(){
			jQuery("#keywordAfterSale").val("");
			afterSaleData.key="";
			afterSaleData.customerId=jQuery("#customerId").val();
			afterSaleTable.draw();
		});
/////////////////查看客户来访信息//////////////////////////////////////////
		var visitTable;///客户来访表格
		var visitData = {};
		jQuery('#t5').on('click',function(){
			if(visitTable!=null) return;//如果visitTable存在直接返回
			var customerId=jQuery('#customerId').val();
			visitData.customerId=customerId;
			visitData.key='';
			visitTable= jQuery('#visitTable').DataTable({
		            "serverSide": true,
		            "iDisplayLength":5,
		            "ordering": false,
		            "ajax": {
		                "dataType": "json",
		                "type": "get",
		                "cache": false,
		                "url":window.rootPath+ '/ui/customerVisiting/getvisitpage',
		                "data": function (d) {
		                    return $.extend({}, d, visitData);
		                }
		            },
		            columns: [
		                {
		                	data: null
		                },
		                {
		                    data: 'customer.customerCode'
		                },
		                {
		                    data: 'customer.customerName'
		                },
		                {
		                    data: 'visitingDate',
		                    render: function (data, type, full, meta) {
		                            if (data)
		                                return moment(data).format('YYYY/MM/DD');
		                            else
		                                return '';
		                     }
		                },
		                {
		                    data: 'project'
		                },
		                {
		                	data: 'contractAmount'
		                },
		                {
		                	data: 'operator'
		                },
		                {
		                	data: 'prove'
		                },
		                {
		                	data: 'memo'
		                }
		                ],
		            "drawCallback": function (oSettings) {
		                var k = 0;
		                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

		                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
		                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
		                    k++;
		                }
		            }
		        });
		})
		//客户来访信息查询
		jQuery('.searchVisitBtn').on('click', function() {
			visitData.key = jQuery('#visitkeyword').val();
			var customerId=jQuery('#customerId').val();
			visitData.customerId=customerId;
			visitTable.draw();
		})
		// 撤销客户来访信息查询
		jQuery('.resetVisitBtn').on("click", function() {
			jQuery('#visitkeyword').val('');
			visitData.key = '';
			var customerId=jQuery('#customerId').val();
			visitData.customerId=customerId;
			visitTable.draw();
		});
		
	
		
		
		
		
		
		
		
		/**
	 * 新增开票信息弹出dialog
	 * */
		jQuery('#pay-add-btn').on("click",function(){
			
			if (jQuery("#customerEdit") && !jQuery("#customerEdit").Validform({
				tiptype : 3,
				showAllError : true
			}).check(false)) {
				return;
			};
			
			jQuery('#payId').val('');
    		jQuery('#identification').val('');
    		jQuery('#invoicePhone').val(jQuery('#cDHPhone').val());
 		   	jQuery('#zipCode').val('');
 		   	jQuery('#invoiceAddress').val(jQuery('#address').val());
        	jQuery('#invoiceBank').val('');
        	jQuery('#bankAccount').val('');
        	jQuery('#invoiceFax').val('');
        	//jQuery('#taxNumber').val('');
        	jQuery('#invoiceMemo').val('');
        	$("#pay-isDefaultHost").val(0);
			$("#pay-isDefaultPart").val(0);
	    	var customerId = jQuery("#customerId").val().trim();
	    	if(!customerId){//如果不存在
	    		
	    		 $.springLemonDialog({
	 		         content: '是否先保存客户信息?',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		        	 if(index==0){
	 		        		saveCustomer(3,"payInfo");
	 		        	 }
	 		        	 
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	    		
	    	}else{
	    		jQuery("#payInfo").modal('show');
	    	}
	    });
		
		
		
		
		 /***
	     * 保存开票信息弹出dialog中的字段内容
	     */ 
	    jQuery('#payInfoSave').on("click",function(){
	    	var CustomerId = jQuery('#customerId').val();
	    	
	    	
	   				if (jQuery("#payInfoForm") && !jQuery("#payInfoForm").Validform({
	   					tiptype : 3,
	   					showAllError : true
	   				}).check(false)) {return;}
	   				
	   				
	   				
	   				
	   				jQuery("#payInfoSave").attr("disabled","disabled");
	   				var api = window.rootPath + "/ui/customer/saveCustomerPayInfo";
	   				var d={
	   						"customer":{"id":CustomerId},
							"id" : $("#payId").val(),
							"identification" : $("#identification").val(),
							"invoicePhone" : $("#invoicePhone").val(),
							"zipCode" : $("#zipCode").val(),
							"invoiceAddress" : $("#invoiceAddress").val(),
							"invoiceBank" : $("#invoiceBank").val(),
							"bankAccount" : $("#bankAccount").val(),
							//"taxNumber" : $("#taxNumber").val(),
							"invoiceFax":$("#invoiceFax").val(), 
							"invoiceMemo" : $("#invoiceMemo").val(),
							"isDefaultHost":$("#pay-isDefaultHost").val(),
							"isDefaultPart":$("#pay-isDefaultPart").val(),
							/**
							 "postUnit" : $("#postUnit").val(),
							"postDelegate" : $("#postDelegate").val(),
							"postPhone" : $("#postPhone").val(),
							"postAddress" : $("#postAddress").val(),
							"postZip" : $("#postZip").val(),
							**/
							
						};
	   				jQuery.ajax({
	   					type : 'post',
	   					url : api,
	   					data : JSON.stringify(d),
	   					dataType : 'json',
	   					contentType : 'application/json;charset=utf-8',
	   					success : function(data) {
//	   						payInfoData.CustomerId=data.customerId;
	   	            		payInfoTable.draw();
	   						jQuery("#payInfoSave").removeAttr("disabled");
	   						jQuery('#payInfo').modal('hide');
	   					}
	   				});
	    });
	    //编辑开票信息
	    jQuery('#pay-edit-btn').on("click",function(){
	        var selects=getSelect("#payInfoTable",1);
	 	   if(selects.length>1){
	 		   $.springLemonDialog({
	 		         content: '只能编辑一条记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	 	   }
	 	   else if(selects.length==0){
	 		   $.springLemonDialog({
	 		         content: '请选择要编辑的记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	 	   }else if(selects.length==1){
	 		   	jQuery('#payId').val(selects[0].id);
	 		   	jQuery('#identification').val(selects[0].identification);
	 		    jQuery('#invoicePhone').val(selects[0].invoicePhone);
	 	    	jQuery('#zipCode').val(selects[0].zipCode);
	 	    	jQuery('#invoiceAddress').val(selects[0].invoiceAddress);
	 	    	jQuery('#invoiceBank').val(selects[0].invoiceBank);
	 	    	jQuery('#bankAccount').val(selects[0].bankAccount);
	 	    	//jQuery('#taxNumber').val(selects[0].taxNumber);
	 	    	jQuery('#invoiceMemo').val(selects[0].invoiceMemo);
	 	    	jQuery('#invoiceFax').val(selects[0].invoiceFax);
	 	    	$("#pay-isDefaultHost").val(selects[0].isDefaultHost);
				$("#pay-isDefaultPart").val(selects[0].isDefaultPart);
	 	    	/**
	 	    	jQuery('#postUnit').val(selects[0].postUnit);
	 	    	jQuery('#postDelegate').val(selects[0].postDelegate);
	 	    	jQuery('#postPhone').val(selects[0].postPhone);
	 	    	jQuery('#postAddress').val(selects[0].postAddress);
	 	    	jQuery('#postZip').val(selects[0].postZip);
	 	    	**/
	 	    	jQuery("#payInfo").modal('show');
	 	   }
	     });
	    //删除开票信息
	    jQuery('#pay-delete-btn').on("click",function(){
	        var selects=getSelect("#payInfoTable",1);
	 	   if(selects.length==0){
	 		   $.springLemonDialog({
	 		         content: '请选择要删除的记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	 	   }else if(selects.length>1){
	 		  $.springLemonDialog({
	 		         content: '每次只能删除一条记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   });
	 	   }else{
	 		   /*var ids= "";
	 		   for(var i=0;i<selects.length;i++){
	 			   ids += selects[i].id + ",";  
	 		   }
	 		   if(ids != ""){
	 			   ids = ids.substring(ids,ids.length-1);
	 		   }*/
	 		   var api=window.rootPath + "/ui/customer/deleteCustomerPayInfo?id="+selects[0].id;
	 		   $.springLemonDialog({
	 		         content: '确认删除这条记录？',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		        	 if (index == 0) {
	 		        		 jQuery.ajax({
	 		        			    url:encodeURI(api),
	 		        	            cache:false,
	 		        	            type :"get",
	 		        	            dataType:"json",
	 		        	            contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
	 		        	            success:function(data){
	 			   	            		payInfoTable.draw();
	 		        	            	/*jQuery('#nTotalContractPrice').val(data.list[0]);
	 		    						jQuery('#nDisCountPercent').val(data.list[1]);*/
	 		        	            }
	 		        	        });	
	 		        	 }
	 		             $(this).closeDialog(modal);
	 		         }
	 		       });
	 	   }
	     });
	    //////开票////
	    
	    
	    
	    
	    
	    
	    
	    
	    
	    
	    
	    
	    //////////////////////////////邮寄////
		/**
	 * 新增邮寄弹出dialog
	 * */
		jQuery('#mailing-add-btn').on("click",function(){
			
			if (jQuery("#customerEdit") && !jQuery("#customerEdit").Validform({
				tiptype : 3,
				showAllError : true
			}).check(false)) {
				return;
			};
		 	jQuery('#mailingId').val('');
        	jQuery('#postUnit').val(jQuery("#customerName").val());
        	jQuery('#postDelegate').val(jQuery('#cDHDB').val());
        	jQuery('#postPhone').val(jQuery('#cDHPhone').val());
        	jQuery('#postAddress').val(jQuery('#address').val());//address
        	jQuery('#postZip').val('');
        	$("#mailing-isDefaultHost").val(0);
			$("#mailing-isDefaultPart").val(0);
	    	var customerId = jQuery("#customerId").val().trim();
	    	if(!customerId){//如果不存在
	    		
	    		 $.springLemonDialog({
	 		         content: '是否先保存客户信息?',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		        	 if(index==0){
	 		        		saveCustomer(3,"mailingInfo");
	 		        	 }
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	    		
	    	}else{
	    		jQuery("#mailingInfo").modal('show');
	    	}
	    });
		
		
		
		
		 /***
	     * 保存邮寄信息弹出dialog中的字段内容
	     */ 
	    jQuery('#mailingInfoSave').on("click",function(){
	    	var CustomerId = jQuery('#customerId').val();
	    	
	    	
	   				if (jQuery("#mailingInfoForm") && !jQuery("#mailingInfoForm").Validform({
	   					tiptype : 3,
	   					showAllError : true
	   				}).check(false)) {return;}
	   				
	   				
	   				
	   				
	   				jQuery("#mailingInfoSave").attr("disabled","disabled");
	   				var api = window.rootPath + "/ui/customer/saveCustomerMailingInfo";
	   				var d={
	   						"customer":{"id":CustomerId},
							"id" : $("#mailingId").val(),
							 "postUnit" : $("#postUnit").val(),
							"postDelegate" : $("#postDelegate").val(),
							"postPhone" : $("#postPhone").val(),
							"postAddress" : $("#postAddress").val(),
							"postZip" : $("#postZip").val(),
							"isDefaultHost":$("#mailing-isDefaultHost").val(),
							"isDefaultPart":$("#mailing-isDefaultPart").val(),
							
						};
	   				jQuery.ajax({
	   					type : 'post',
	   					url : api,
	   					data : JSON.stringify(d),
	   					dataType : 'json',
	   					contentType : 'application/json;charset=utf-8',
	   					success : function(data) {
//	   						payInfoData.CustomerId=data.customerId;
	   	            		mailingInfoTable.draw();
	   						jQuery("#mailingInfoSave").removeAttr("disabled");
	   						jQuery('#mailingInfo').modal('hide');
	   					}
	   				});
	    });
	    //编辑邮寄信息
	    jQuery('#mailing-edit-btn').on("click",function(){
	        var selects=getSelect("#mailingInfoTable",1);
	 	   if(selects.length>1){
	 		   $.springLemonDialog({
	 		         content: '只能编辑一条记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	 	   }
	 	   else if(selects.length==0){
	 		   $.springLemonDialog({
	 		         content: '请选择要编辑的记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	 	   }else if(selects.length==1){
	 		   	jQuery('#mailingId').val(selects[0].id);
	 	    	jQuery('#postUnit').val(selects[0].postUnit);
	 	    	jQuery('#postDelegate').val(selects[0].postDelegate);
	 	    	jQuery('#postPhone').val(selects[0].postPhone);
	 	    	jQuery('#postAddress').val(selects[0].postAddress);
	 	    	jQuery('#postZip').val(selects[0].postZip);
	 	    	$("#mailing-isDefaultHost").val(selects[0].isDefaultHost);
				$("#mailing-isDefaultPart").val(selects[0].isDefaultPart);
	 	    	jQuery("#mailingInfo").modal('show');
	 	   }
	     });
	    //删除邮寄信息
	    jQuery('#mailing-delete-btn').on("click",function(){
	        var selects=getSelect("#mailingInfoTable",1);
	 	   if(selects.length==0){
	 		   $.springLemonDialog({
	 		         content: '请选择要删除的记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	 	   }else if(selects.length>1){
	 		  $.springLemonDialog({
	 		         content: '每次只能删除一条记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   });
	 	   }else{
	 		   /*var ids= "";
	 		   for(var i=0;i<selects.length;i++){
	 			   ids += selects[i].id + ",";  
	 		   }
	 		   if(ids != ""){
	 			   ids = ids.substring(ids,ids.length-1);
	 		   }*/
	 		   var api=window.rootPath + "/ui/customer/deleteCustomerMailingInfo?id="+selects[0].id;
	 		   $.springLemonDialog({
	 		         content: '确认删除这条记录？',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		        	 if (index == 0) {
	 		        		 jQuery.ajax({
	 		        			    url:encodeURI(api),
	 		        	            cache:false,
	 		        	            type :"get",
	 		        	            dataType:"json",
	 		        	            contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
	 		        	            success:function(data){
	 			   	            		mailingInfoTable.draw();
	 		        	            	/*jQuery('#nTotalContractPrice').val(data.list[0]);
	 		    						jQuery('#nDisCountPercent').val(data.list[1]);*/
	 		        	            }
	 		        	        });	
	 		        	 }
	 		             $(this).closeDialog(modal);
	 		         }
	 		       });
	 	   }
	     });
	    
	    //////////////////////////////邮寄 
	    
	    
	    
	    //////////////////////////////发货////
	    /**
	     * 新增发货弹出dialog
	     * */
	    jQuery('#send-add-btn').on("click",function(){
	    	
	    	if (jQuery("#customerEdit") && !jQuery("#customerEdit").Validform({
	    		tiptype : 3,
	    		showAllError : true
	    	}).check(false)) {
	    		return;
	    	};
	    	jQuery('#sendId').val('');
	    	jQuery('#cUnitName').val(jQuery('#customerName').val());//customerName
	    	jQuery('#cSHYB').val('');
	    	jQuery('#cSHName').val(jQuery('#cDHDB').val());
	    	jQuery('#cSHPhoneName').val(jQuery('#cDHPhone').val());//cDHPhone
	    	jQuery('#cPhoneNumber').val(jQuery('#address').val());
	    	$("#send-isDefaultHost").val(0);
			$("#send-isDefaultPart").val(0);
	    	var customerId = jQuery("#customerId").val().trim();
	    	if(!customerId){//如果不存在
	    		
	    		$.springLemonDialog({
	    			content: '是否先保存客户信息?',// 确认文本
	    			clickButton: function (sender, modal, index) {// 按钮事件处理
	    				if(index==0){
	    					saveCustomer(3,"sendInfo");
	    				}
	    				$(this).closeDialog(modal);
	    			}
	    		}); 
	    		
	    	}else{
	    		jQuery("#sendInfo").modal('show');
	    	}
	    });
	    
	    
	    
	    
	    /***
	     * 保存发货信息弹出dialog中的字段内容
	     */ 
	    jQuery('#sendInfoSave').on("click",function(){
	    	var CustomerId = jQuery('#customerId').val();
	    	
	    	
	    	if (jQuery("#sendInfoForm") && !jQuery("#sendInfoForm").Validform({
	    		tiptype : 3,
	    		showAllError : true
	    	}).check(false)) {return;}
	    	
	    	
	    	
	    	
	    	jQuery("#sendInfoSave").attr("disabled","disabled");
	    	var api = window.rootPath + "/ui/customer/saveCustomerSendInfo";
	    	var d={
	    			"customer":{"id":CustomerId},
	    			"id" : $("#sendId").val(),
	    			"cUnitName" : $("#cUnitName").val(),
	    			"cSHYB" : $("#cSHYB").val(),
	    			"cSHName" : $("#cSHName").val(),
	    			"cSHPhoneName" : $("#cSHPhoneName").val(),
	    			"cPhoneNumber" : $("#cPhoneNumber").val(),
					"isDefaultHost":$("#send-isDefaultHost").val(),
					"isDefaultPart":$("#send-isDefaultPart").val(),
	    			
	    	};
	    	jQuery.ajax({
	    		type : 'post',
	    		url : api,
	    		data : JSON.stringify(d),
	    		dataType : 'json',
	    		contentType : 'application/json;charset=utf-8',
	    		success : function(data) {
//	   						payInfoData.CustomerId=data.customerId;
	    			sendInfoTable.draw();
	    			jQuery("#sendInfoSave").removeAttr("disabled");
	    			jQuery('#sendInfo').modal('hide');
	    		}
	    	});
	    });
	    //编辑发货信息
	    jQuery('#send-edit-btn').on("click",function(){
	    	var selects=getSelect("#sendInfoTable",1);
	    	if(selects.length>1){
	    		$.springLemonDialog({
	    			content: '只能编辑一条记录',// 确认文本
	    			clickButton: function (sender, modal, index) {// 按钮事件处理
	    				$(this).closeDialog(modal);
	    			}
	    		}); 
	    	}
	    	else if(selects.length==0){
	    		$.springLemonDialog({
	    			content: '请选择要编辑的记录',// 确认文本
	    			clickButton: function (sender, modal, index) {// 按钮事件处理
	    				$(this).closeDialog(modal);
	    			}
	    		}); 
	    	}else if(selects.length==1){
	    		jQuery('#sendId').val(selects[0].id);
	    		jQuery('#cUnitName').val(selects[0].cUnitName);
	    		jQuery('#cSHYB').val(selects[0].cSHYB);
	    		jQuery('#cSHName').val(selects[0].cSHName);
	    		jQuery('#cSHPhoneName').val(selects[0].cSHPhoneName);
	    		jQuery('#cPhoneNumber').val(selects[0].cPhoneNumber);
	 	    	$("#send-isDefaultHost").val(selects[0].isDefaultHost);
				$("#send-isDefaultPart").val(selects[0].isDefaultPart);
	    		jQuery("#sendInfo").modal('show');
	    	}
	    });
	    //删除发货信息
	    jQuery('#send-delete-btn').on("click",function(){
	    	var selects=getSelect("#sendInfoTable",1);
	    	if(selects.length==0){
	    		$.springLemonDialog({
	    			content: '请选择要删除的记录',// 确认文本
	    			clickButton: function (sender, modal, index) {// 按钮事件处理
	    				$(this).closeDialog(modal);
	    			}
	    		}); 
	    	}else if(selects.length>1){
	    		$.springLemonDialog({
	    			content: '每次只能删除一条记录',// 确认文本
	    			clickButton: function (sender, modal, index) {// 按钮事件处理
	    				$(this).closeDialog(modal);
	    			}
	    		});
	    	}else{
	    		/*var ids= "";
	 		   for(var i=0;i<selects.length;i++){
	 			   ids += selects[i].id + ",";  
	 		   }
	 		   if(ids != ""){
	 			   ids = ids.substring(ids,ids.length-1);
	 		   }*/
	    		var api=window.rootPath + "/ui/customer/deleteCustomerSendInfo?id="+selects[0].id;
	    		$.springLemonDialog({
	    			content: '确认删除这条记录？',// 确认文本
	    			clickButton: function (sender, modal, index) {// 按钮事件处理
	    				if (index == 0) {
	    					jQuery.ajax({
	    						url:encodeURI(api),
	    						cache:false,
	    						type :"get",
	    						dataType:"json",
	    						contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
	    						success:function(data){
	    							sendInfoTable.draw();
	    							/*jQuery('#nTotalContractPrice').val(data.list[0]);
	 		    						jQuery('#nDisCountPercent').val(data.list[1]);*/
	    						}
	    					});	
	    				}
	    				$(this).closeDialog(modal);
	    			}
	    		});
	    	}
	    });
	    
	    //////////////////////////////
	    
	    
	    
	    
});
